/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.jsfconsole.util;

/**
 * Helps us work with string
 * @author gbugaj
 *
 */
public class StringUtil {
	/**
	 * Trim String to given size 
	 * Ex:  trimToSize("Hello Greg", 5, true)=Hello...
	 * Ex:  trimToSize("Hello Greg", 5, false)=Hello
	 * @param string
	 * @param size
	 * @param withElipse
	 * @return trim string to size with eclipse or without
	 */
	public static String trimToSize (String string, Integer size, Boolean withElipse) {
		if(string==null) return "";		
		if(size >= string.length()) return string;
		String sub=string.substring(0, size);
		return (withElipse?sub+"...":sub);
	}
	
	/**
	 * Check if the value is Null or empty
	 * @param value to check
	 * @return true if the string is empty or NULL
	 */
	public static boolean isNullOrEmpty(String value){
		return (null == value || value.trim().equals(""));
	}
	
	/**
	 * Method the encode a string
	 * @param inputString
	 * @return
	 */
	public static String escapeString (String inputString) {
		if(!isNullOrEmpty(inputString)) {
			/*try {
				return java.net.URLEncoder.encode(inputString, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				return inputString;
			}*/
			inputString=inputString.replace("\\","\\\\");
			inputString=inputString.replaceAll("'","\\\\'");
		}
		return inputString;
	}
	
}
